Read the data from the database
library(RMySQL)
## Warning: package 'RMySQL' was built under R version 3.2.3
## Loading required package: DBI
## Warning: package 'DBI' was built under R version 3.2.3
mydb = dbConnect(MySQL(), user='root', password='', dbname='pgs', host='localhost')
turbineData <- dbGetQuery(mydb, "Select * from turbineData order by timestamp")
## Warning in .local(conn, statement, ...): Decimal MySQL column 2 imported as
## numeric
## Warning in .local(conn, statement, ...): Decimal MySQL column 3 imported as
## numeric
## Warning in .local(conn, statement, ...): Decimal MySQL column 4 imported as
## numeric
## Warning in .local(conn, statement, ...): Decimal MySQL column 5 imported as
## numeric
## Warning in .local(conn, statement, ...): Decimal MySQL column 6 imported as
## numeric
str(turbineData)
## 'data.frame': 119915 obs. of 7 variables:
## $ timestamp : chr "2015-06-01 00:00:00" "2015-06-01 00:00:00" "2015-06-01 00:00:00" "2015-06-01 00:00:00" ...
## $ turbineId : chr "WTG011" "WTG012" "WTG010" "WTG031" ...
## $ power : num 0.81 0 16.62 0 108.83 ...
## $ windspeed : num 3.56 3.02 3.64 4.32 4.6 4.89 4.02 4.55 4.92 3.64 ...
## $ nacelle : num 196 204 177 232 172 ...
## $ temperature : num 23.6 23.4 23.5 25 24.1 ...
## $ operatingState: num 99.9 99.9 99.9 99.9 16 16 16 99.9 16 99.9 ...
curveIndexes1<-seq(from=1,to=9,by=1)
#curveIndexes2<-c(10:20,22,23,25:28,31,34,38,40:43,45:52,55,56)
curveIndexes2<-seq(from=10,to=56,by=1)
curveIndexesCombined=c(paste0("WTG00",1:9),paste0("WTG0",10:20),"WTG022","WTG023",
paste0("WTG0",25:28),"WTG031","WTG034","WTG038",paste0("WTG0",40:43),
paste0("WTG0",45:48),"WTG050","WTG051","WTG052","WTG055","WTG056")
p1=paste0("WTG00",curveIndexes1)
p2=paste0("WTG0",curveIndexes2)
curveIndexes=c(p1,p2)
curveIndexes1=as.factor(curveIndexes)
curveIndexes1=curveIndexes
numberOfTurbines=length(curveIndexesCombined)
curveNames=curveIndexesCombined
Define the function to get the compass directions
getCompassDirection=function(heading){
if (is.na(heading))
NA
if(heading>348.75 & heading <360.1)
"N"
else if(heading>=0 & heading <11.25)
"N"
else if(heading>11.25 & heading <33.75)
"NNE"
else if(heading>33.75 & heading <56.25)
"NE"
else if(heading>56.25 & heading <78.75)
"ENE"
else if(heading>78.75 & heading <101.25)
"E"
else if(heading>101.25 & heading <123.75)
"ESE"
else if(heading>123.75 & heading <146.25)
"SE"
else if(heading>146.25 & heading <168.75)
"SSE"
else if(heading>168.75 & heading <191.25)
"S"
else if(heading>191.25 & heading <213.75)
"SSW"
else if(heading>213.75 & heading <236.25)
"SW"
else if(heading>236.25 & heading <258.75)
"WSW"
else if(heading>258.75 & heading <281.25)
"W"
else if(heading>281.25 & heading <303.75)
"WNW"
else if(heading>303.75 & heading <326.25)
"NW"
else if(heading>326.25 & heading <348.75)
"NNW"
}
meanDirection=function(x)
{
#names(sort(table(x),decreasing = TRUE))[1]
sort(table(x),decreasing = TRUE)
}
changingDirection=function(x)
{
ll=length(names(sort(table(x))))
ss=sum(table(x))
as.character(c(ll,":",ss))
}
countUniqueDirections=function(x)
{
ll=length(names(sort(table(x))))
return(ll);
}
countNumberOfReadings=function(x)
{
ss=sum(table(x))
return(ss)
}
myMean=function(x)
{
ss=sum(table(x))
#return(ll);
return(sum(x)/ss)
}
df=turbineData
df$compassDirection=sapply(df$nacelle,getCompassDirection)
tt<-as.Date(df$timestamp)
df$days=as.factor(format(tt,'%d'))
#draw power plots of various turbines
for(i in c(1:numberOfTurbines)){
individualTurbineData=subset(df,df$turbineId==curveIndexesCombined[i])
unsortedPowerByDay=by(individualTurbineData$power,individualTurbineData$days,sum)
barplot(unsortedPowerByDay,main = paste0("power curve for ",curveNames[i]))
#individualTurbineData$compassDirection=as.factor(individualTurbineData$compassDirection)
#similarly for the wind speed
unsortedAverageWindSpeedByDay=by(individualTurbineData$windspeed,individualTurbineData$days,myMean)
barplot(unsortedAverageWindSpeedByDay,main = paste0("wind curve for ",curveNames[i]))
#direction distributions
# windDirectionperDay=by(individualTurbineData$compassDirection,individualTurbineData$day#s,meanDirection)
plot(individualTurbineData$days,individualTurbineData$power,main=curveIndexesCombined[i])
library(plotrix)
spread=max(individualTurbineData$nacelle)-min(individualTurbineData$nacelle)
label=paste(curveNames[i],spread)
polar.plot(individualTurbineData$windspeed,polar.pos=individualTurbineData$nacelle,
start=90,clockwise=TRUE,rp.type="s",
point.symbols=19,boxed.radial=FALSE,
radial.labels="",main = label,lwd=2,line.col=4)
}
## Warning: package 'plotrix' was built under R version 3.2.3
Importnat thing to note is that in this case the nacelled is restricted to only one section